package com.esun.cheetah.biz.personal.personalcommunicater;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.util.PushUtils;
import com.esun.util.VersionUtils;
import com.esun.util.log.EDebug;

/**
 * 
 * @Package com.esun.cheetah.biz.personal.personalcommunicater.PersonalLoginCommunicater
 * @ClassName: PersonalLoginCommunicater
 * @Description: TODO(user send login information)
 * @author handy <br/>
 * @date create at 2012-7-31 下午3:11:21
 *
 */
public class PersonalLoginCommunicater extends AbsInferiorityCommunicater {

	private String status;// 状态
	private String msg;// 返回信息
	private String money;
	private String freezemoney;
	private String ck;
	private String bindmobile;

	private String username = "";
	private String userpass = "";
	private Context context;

	public PersonalLoginCommunicater(Handler handler, String username, String userpass, Context context) {
		super(handler);
		this.username = username;
		this.userpass = userpass;
		this.setContext(context);
//		PushUtils.isNewUUId(context);
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> hmJson = new HashMap<String, String>();
		hmJson.put(Key.KEY_ID, BizCode.BIZCODE_PERSONAL_LOGIN_PROTECT);
		hmJson.put(Key.KEY_USERPASS, userpass);
		hmJson.put(Key.KEY_USERNAME, username);
		hmJson.put(Key.PLATFROM, BizCode.ZERO);
		hmJson.put(Key.IMEI, PushUtils.getPushUnique());
		hmJson.put(Key.KEY_SERVIAL, VersionUtils.getInstance().getSerial(context));
		return hmJson;
	}

	@Override
	public void getJsonContent(String serverRes) throws JSONException {

		JSONObject json = new JSONObject(serverRes);
		status = json.getString(Key.STATUS);
		msg = json.getString(Key.MSG);

		EDebug.o("msg---" + msg);
		EDebug.o("status---" + status);
		if (Key.TRUE.equals(status)) {
			JSONObject jsondata = new JSONObject(json.getString(Key.DATA));
			username = jsondata.getString(Key.KEY_USERNAME);
			EDebug.o("username---" + username);
			money = jsondata.getString(Key.MONEY);
			EDebug.o("money---" + money);
			freezemoney = jsondata.getString(Key.FREEZEMONEY);
			EDebug.o("freezemoney----" + freezemoney);
			ck = jsondata.getString(Key.CK);
			EDebug.o("ck----" + ck);
			bindmobile = jsondata.getString("bindmobile");
			EDebug.o("bindmobile---" + bindmobile);
			saveUserInfo();
		}
	}

	private void saveUserInfo() {
		UserInfo.getUserInfo().setCk(ck);
		UserInfo.getUserInfo().setUserName(username);
		UserInfo.getUserInfo().setMoney(money);
		UserInfo.getUserInfo().setFreezemoney(freezemoney);
		UserInfo.getUserInfo().setUserpwdMD5(userpass);
		UserInfo.getUserInfo().setBindmobile(bindmobile);
		EDebug.o("md5  ----" + UserInfo.getUserInfo().getUserpwdMD5());
	}

	public Context getContext() {
		return this.context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	@Override
	public void getServerResponseFail(String serverRes) throws JSONException {
		
		JSONObject json = new JSONObject(serverRes);
		JSONObject jsondata = new JSONObject(json.getString(Key.DATA));
		
		if (jsondata != null && jsondata.has(Key.KEY_COUNT)) {
			Message msg = handlerFromAct.obtainMessage(Config.COMMUNICATER_FAIL);
			msg.obj = jsondata.getString(Key.KEY_COUNT) ;
			handlerFromAct.sendMessage(msg);
		} else {
			Message msg = handlerFromAct.obtainMessage(Config.COMMUNICATER_FAIL);
			handlerFromAct.sendMessage(msg);
		}
	}

}
